package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import com.dudu.model.ImsCharge;

import net.sf.json.JSONObject;
import core.CoreDao;
import core.db.DBConnectionUtil;

public class ImsChargeDao {
	
	/**
	 * 插入数据
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int insert(CoreDao dao, ImsCharge bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("INSERT INTO t_ims_charge( app_id, charge_no, charge_key, effect_time, points)");
			sql.append(" VALUES(?,?,?,?,?)");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
							bean.getApp_id(),
							bean.getCharge_no(),
							bean.getCharge_key(),
							bean.getEffect_time(),
							bean.getPoints()
							});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 更新数据
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int update(CoreDao dao, ImsCharge bean) {

		Connection con = null;
		int result = -1;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" UPDATE t_ims_charge set effect_time=?, points=?");
			sql.append(" where app_id=? and charge_no=?");

			List<JSONObject> list = dao.serviceJsonSql(

					con,
					sql.toString(),
					new Object[] {  
						
						bean.getEffect_time(),
						bean.getPoints(),
						bean.getApp_id(),
						bean.getCharge_no()
					
					}

			);

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	/**
	 * 查询数据
	 * @param dao
	 * @param bean
	 * @return
	 */
	public ImsCharge query(CoreDao dao, ImsCharge bean) {

		Connection con = null;
		ImsCharge result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"select id FROM t_ims_charge where app_id=? and charge_no=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] {
				
				bean.getApp_id(),
				bean.getCharge_no()
				
			});
			if (list != null && list.size() > 0) {
				List<ImsCharge> items = dao.covertToBean(list, ImsCharge.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	
	
	
}
